import bentoml

def main():
    models = list(bentoml.models.list())
    by_name = {}
    for m in models:
        by_name.setdefault(m.tag.name, []).append(m)
    for name, items in by_name.items():
        items.sort(key=lambda x: x.creation_time, reverse=True)
        latest_version = None
        try:
            latest_version = bentoml.models.get(f"{name}:latest").tag.version
        except Exception:
            pass
        print(name)
        for m in items:
            marker = " <- latest" if latest_version and m.tag.version == latest_version else ""
            print(f"  {m.tag}{marker}")
    return 0

if __name__ == "__main__":
    raise SystemExit(main())

